/*
 * ksBlock.h
 *
 *  Created on: Aug 3, 2011
 *      Author: wang
 */

#ifndef KSBLOCK_H_
#define KSBLOCK_H_
#include "ksDefines.h"
#include <iostream>
#include <iosfwd>

class ksBlockReader
{
public:
    virtual void load(std::ifstream& in) = 0;
    virtual void extract(const char *path) = 0;
};

class ksBlockWriter
{
public:
    virtual unsigned int blockLength() = 0;
    virtual void save(std::ofstream& out) = 0;
    virtual void prepare(int blockPositionInFile) = 0;
};

class ksBlock
{
public:
    ksBlock();
    virtual ~ksBlock();
protected:
    struct _atomBlockHeader
    {
        unsigned short type;
        unsigned short id;
        unsigned char compress;
        unsigned char encrypt;
        unsigned char placeHole[2];
        unsigned int dataLength;
    };
    struct _containerBlockHeader
    {
        unsigned short type;
        unsigned short id;
        unsigned char compress;
        unsigned char encrypt;
        unsigned short entryCount;
        unsigned int entryTableSize;
        unsigned int dataLength;
        unsigned int reserve;
    };

    typedef union _blockHeader
    {
        _atomBlockHeader atom;
        _containerBlockHeader container;
    } BlockHeader;
protected:
    BlockHeader _blockHeader;
public:
    unsigned char compress() const;
    unsigned char encrypt() const;
    unsigned short id() const;
    unsigned short type() const;
    void compress(unsigned char compress);
    void encrypt(unsigned char encrypt);
    void id(unsigned short);
    void type(unsigned short);
public:
    virtual ksBlockReader* reader()
    {
        return NULL;
    }
    virtual ksBlockWriter* writer()
    {
        return NULL;
    }
};

#endif /* KSBLOCK_H_ */
